package leducHoldem.misc;

import java.util.LinkedList;
import java.util.Random;

import leducHoldem.Data.Board;
import leducHoldem.Data.Dealer;
import leducHoldem.Data.Move;

public class ChanceNode extends Node {

	public ChanceNode(Node parent, Board board, Move move, Dealer dealer) {		
		super(parent, board, move, dealer);
		type = "chance";
	}

	@Override
	public Node getBestChild(byte sign) {
		
		if (children == null) {
			return null;
		}

		float move = new Random().nextFloat();
		float[] probabilities = dealer.getCardProbabilities();

		for (int i = 0; i < probabilities.length; i++) {
			move -= probabilities[i];

			if (move < 0) {
				return children.get(i);
			}

		}

		return children.getLast();
	}

	@Override
	public void generateChildren() {

		Node node;
		children = new LinkedList<Node>();

		for (Move move : dealer.getCardTypes()) {

			board.makeMove(move);
			dealer.removeCard(move);
			node = new Node(this, new Board(board), move, new Dealer(dealer));
			
			children.add(node);

			board.reverseMove();
			dealer.returnCard(move);

		}

		board = null;
	}

}
